package com.css.fxfzmh.zcpt.sys.service.impl;

import com.alibaba.fastjson.JSON;
import com.css.fxfzmh.zcpt.sys.entity.SUser;
import com.css.fxfzmh.zcpt.sys.entity.UserEntity;
import com.css.fxfzmh.zcpt.sys.entity.vo.SUserVO;
import com.css.fxfzmh.zcpt.sys.entity.vo.UpdatePasswordVO;
import com.css.fxfzmh.zcpt.sys.service.SUserService;
import com.css.fxfzmh.util.*;
import org.slw.common.helper.PropsHelper;
import org.slw.common.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@Service
public class SUserServiceImpl implements SUserService {

    @Autowired
    private  RestTemplateUtil restTemplateUtil;

    /**
     * 获取用户信息
     */
    @Override
    public String getSUser(String userId) {
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String url = configGroup.getProperty("baseRestUrl") + "/suser_dzj/getUser?userId="+userId;
        Map<String, Object> paramMap = new HashMap<>(2);
        paramMap.put("userId",userId);
        ResponseEntity<String> responseEntity = restTemplateUtil.sendHttpRequest(url, paramMap);
        return  responseEntity.getBody();
    }


    @Override
    public String queryUsersByOrg(String orgId) {
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String url = configGroup.getProperty("baseRestUrl") + "/suser_dzj/queryUsersByOrg?orgId="+orgId;
        Map<String, Object> paramMap = new HashMap<>(2);
        paramMap.put("orgId",orgId);
        ResponseEntity<String> responseEntity = restTemplateUtil.sendHttpRequest(url, paramMap);
        return  responseEntity.getBody();
    }

    @Override
    public String queryUsersByOrg1(String orgId, String userName, Integer pageSize, Integer currentPage) throws UnsupportedEncodingException {
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String url = configGroup.getProperty("baseRestUrl") + "/suser_dzj/pageUsersByOrgAndUserName?orgId="+orgId+"&userName="+ URLEncoder.encode(userName,"UTF-8") +"&pageSize="+pageSize+"&currentPage="+currentPage;
        Map<String, Object> paramMap = new HashMap<>(2);
        paramMap.put("orgId",orgId);
        paramMap.put("userName",URLEncoder.encode(userName,"UTF-8"));
        paramMap.put("pageSize",pageSize);
        paramMap.put("currentPage",currentPage);
        ResponseEntity<String> responseEntity = restTemplateUtil.sendHttpRequest(url, paramMap);
        return  responseEntity.getBody();
    }

    @Override
    public String updateUserPassword(UpdatePasswordVO vo) throws Exception {
        //读取配置文件
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String url = configGroup.getProperty("baseRestUrl") + "/suser_dzj/updUserPassword";
        Map<String, Object> paramMap = new HashMap<>(1);
        paramMap.put("userId",vo.getUserId());
        paramMap.put("oldPassword", vo.getOldPassword());
        paramMap.put("password",vo.getPassword());
        paramMap.put("salt","");
        ResponseEntity<String> stringResponseEntity = restTemplateUtil.sendPostHttpRequest(url, paramMap);
        return stringResponseEntity.getBody();
    }

    @Override
    public String getUserById(String userId) {
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String url = configGroup.getProperty("baseRestUrl") + "/suser_dzj/getUserById?userId="+userId;
        Map<String, Object> paramMap = new HashMap<>(2);
        paramMap.put("userId",userId);
        ResponseEntity<String> responseEntity = restTemplateUtil.sendHttpRequest(url, paramMap);
        return  responseEntity.getBody();
    }

    @Override
    public Object getFwUserByCode(String userCode) {
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String url = configGroup.getProperty("fwRestUrl") + "?code="+userCode;
        //调用工具类
        String jsonStr = ConnectUtil.connectGet(url);
        //接收返回数据
        Map map= JSON.parseObject(jsonStr);
        if(PlatformObjectUtils.isNotEmpty(map)){
            return map.get("data");
        }else{
            return null;
        }

    }

    @Override
    public Object getJgUserByCode(String userCode) {
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String url = configGroup.getProperty("jgRestUrl") + "?code="+userCode;
        //String shieldDataUrl="http://10.13.155.116:8080/platform/org/users/getUserByCode";

        //调用工具类
        String jsonStr = ConnectUtil.connectGet(url);
        //String jsonStr = ConnectUtil.connectGet(shieldDataUrl+"/?code="+userCode);
        //接收返回数据
        Map map= JSON.parseObject(jsonStr);
        if(PlatformObjectUtils.isNotEmpty(map)){
            return map.get("data");
        }else{
            return null;
        }

    }

    @Override
    public Object getRkUserByCode(String userCode) {
//        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
//        String url = configGroup.getProperty("rkRestUrl") + "/getuserbycode?userCode="+userCode;
//        //String shieldDataUrl="http://10.13.155.116:8080/platform/org/users/getUserByCode";
//
//        //调用工具类
//        String jsonStr = ConnectUtil.connectGet(url);
//        //String jsonStr = ConnectUtil.connectGet(shieldDataUrl+"/?code="+userCode);
//        //接收返回数据
//        Map map= JSON.parseObject(jsonStr);
//        if(PlatformObjectUtils.isNotEmpty(map)){
//            return map.get("data");
//        }else{
//            return null;
//        }
        return null;
    }

    @Override
    public String addSuser(SUserVO sUser) throws Exception {
        //读取配置文件
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String url = configGroup.getProperty("baseRestUrl") + "/suser_dzj/addOrUpdateSUserMH";
        Map<String, Object> paramMap = new HashMap<>(1);
//        paramMap.put("user", sUser);
        //paramMap.put("userName",sUser.getUserName());
        String defaultDept = configGroup.getProperty("defaultDept");
        if(PlatformObjectUtils.isNotEmpty(defaultDept)){
            sUser.setOrgId(defaultDept);
        }
        else{
            throw new Exception("获取默认组织结构失败,请检查配置文件!");
        }
        paramMap.put("password", Md5Util.getMD5(sUser.getPassword()));
        paramMap.put("loginName",sUser.getUserCode());
        paramMap.put("sex",sUser.getSex());
        paramMap.put("mobile",sUser.getMobilePhone());
        paramMap.put("realName",URLEncoder.encode(sUser.getUserName(),"utf-8"));
        paramMap.put("fax",sUser.getFax());
        paramMap.put("email",sUser.getEmail());
        paramMap.put("officePhone",sUser.getOfficePhone());
        //paramMap.put()
        paramMap.put("orgId",sUser.getOrgId());
        paramMap.put("provinceId",sUser.getProvinceId());
        paramMap.put("cityId",sUser.getCityId());
        paramMap.put("countyId",sUser.getCountyId());
        ResponseEntity<String> stringResponseEntity = restTemplateUtil.sendPostHttpRequest(url, paramMap);
        return stringResponseEntity.getBody();
    }

    @Override
    public SUser getYwUserById(String userId){
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String url = configGroup.getProperty("jgRestUrl") + "/?id="+userId;
        //String shieldDataUrl="http://10.13.155.116:8080/platform/org/users/getUserByCode";
        //调用工具类
        try{
            String jsonStr = ConnectUtil.connectGet(url);
            SUser jgUser = JSON.parseObject(jsonStr, SUser.class);
            if(PlatformObjectUtils.isNotEmpty(jgUser)){
                return jgUser;
            }
        }catch (Exception e){
            try{
                url = configGroup.getProperty("fwRestUrl") + "/?id="+userId;
                String jsonStr = ConnectUtil.connectGet(url);
                SUser fwUser = JSON.parseObject(jsonStr, SUser.class);

                if(PlatformObjectUtils.isNotEmpty(fwUser)){
                    return fwUser;
                }
            }catch (Exception e1){
                 try {
                     url = configGroup.getProperty("rkRestUrl") + "/getuserbyid?userId="+userId;
                     String jsonStr = ConnectUtil.connectGet(url);
                     SUser rkUser = JSON.parseObject(jsonStr, SUser.class);

                     if (PlatformObjectUtils.isNotEmpty(rkUser)){
                         return rkUser;
                     }
                 }catch (Exception e2){
                     return  null ;
                 }
            }
        }
        return  null;
    }
}
